package com.wspyydshhh.mytool.util;

import com.wspyydshhh.mytool.pojo.RecordPojo;
import ohos.data.distributed.common.Entry;
import ohos.data.distributed.common.Query;
import ohos.data.distributed.user.SingleKvStore;
import ohos.utils.zson.ZSONObject;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

public class DataUtil {
    private volatile List<RecordPojo> recordList;
    private volatile List<RecordPojo> unRecordList;
    public void updataList(SingleKvStore singleKvStore) {
        //获取未领取任务
        ZSONObject zsonObject = null;
        Query query = Query.select();
        query.equalTo("$.hasTask",0)
                .and()
                .equalTo("$.isAdd",false);
        List<Entry> entries = singleKvStore.getEntries(query);
        //将entries赋值到unRecordList
        System.out.println("okkkkkkkkkkkkkkkkkkkk"+entries.size());
        int i=0;
        RecordPojo pojo = null;
        for (Entry entry : entries) {
            zsonObject = ZSONObject.stringToZSON(entry.getValue().getString());
            pojo = new RecordPojo();
            pojo.setTitle(zsonObject.getString("title"));
            pojo.setAdd(false);
            pojo.setDay(zsonObject.getInteger("day"));


            recordList.add(pojo);
            i++;
        }
        query.reset();
        //获取完成的任务及提交的预算
        query.equalTo("$.hasTask",1);
        entries = singleKvStore.getEntries(query);
        //将entries赋值到unRecordListunRecordList

    }

    //给日期时间获取时间戳
    public static long getTimestamOfDate(int y,int m,int d,int h,int mim,int s){
        LocalDateTime dateTime = LocalDateTime.of(y, m, d, h, mim, s);
        return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    public synchronized List<RecordPojo> getUnRecordList() {
        return unRecordList;
    }

    public synchronized void setUnRecordList(List<RecordPojo> unRecordList) {
        this.unRecordList = unRecordList;
    }

    public synchronized List<RecordPojo> getRecordList() {
        return recordList;
    }

    public synchronized void setRecordList(List<RecordPojo> recordList) {
        this.recordList = recordList;
    }
}
